<?php

namespace App\Http\Controllers\Admin\Cashier;

use App\Models\Shop\Mark;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cashier\Purchase;
use App\Models\Cashier\Category;
use App\Models\Cashier\Brand;
use App\Models\Cashier\Store;
use App\Models\Cashier\Product;

class PurchaseController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_cashier' => 'active',
            '_work' => 'active open',
            '_purchase' => 'active',
            'categories' => Category::get_categories(),
            'brands' => Brand::orderBy('sort_order')->get(),
            'stores' => Store::orderBy('id')->get(),
            'filter_categories' => Category::filter_categories(),
        ]);

    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $where = function ($query) use ($request) {
            //按商品名称
            if ($request->has('key') and $request->key != '') {
                $search = "%" . $request->key . "%";
                $query->where('name', 'like', $search);
            }

            //按品牌
            if ($request->has('brand_id') and $request->brand_id != '-1') {
                $query->where('brand_id', $request->brand_id);
            }

            //按分类
            if ($request->has('category_id') and $request->category_id != '-1') {
                $query->where('category_id', $request->category_id);
            }
        };
        $purchases = Purchase::with('brand', 'category', 'store')->where($where)->orderBy('sort_order', 'desc')->paginate(env('pageSize'));
        return view('admin.cashier.purchase.index', compact('purchases'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.cashier.purchase.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|max:255',
            'stock' => 'required|integer',
            'sort_order' => 'required|integer|max:99',
            'description' => 'required',
            'store_id' => 'required'
        ]);

        //如果新录入的商品，商品表里已经存在，那么只需要更新商品的库存，否则执行插入
        $product = Product::where('store_id', $request->store_id)->where('code', $request->code)->first();
        if ($product) {
            Product::where('store_id', $request->store_id)->where('code', $request->code)
                ->increment('stock');
        }

        Purchase::create($request->all());
        Product::create($request->all());

        return redirect(route('cashier.purchase.index'))->with('success', '商品录入成功~');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $purchase = Purchase::find($id);
        return view('admin.cashier.purchase.edit', compact('purchase'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required|max:255',
            'stock' => 'required|integer',
            'sort_order' => 'required|integer|max:99',
            'description' => 'required'
        ]);

        $purchase = Purchase::find($id);
        $purchase->update($request->all());
        return redirect(route('cashier.purchase.index'))->with('success', '编辑商品成功~');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Purchase::destroy($id);
        return back()->with('success', '删除商品成功~');
    }


    /** ajax排序
     * @param Request $request
     */
    public function sort_order(Request $request)
    {
        $product = Purchase::find($request->id);
        $product->sort_order = $request->sort_order;
        $product->save();
    }


    /** ajax修改库存
     * @param Request $request
     */
    public function stock(Request $request)
    {
        $product = Purchase::find($request->id);
        $product->stock = $request->stock;
        $product->save();
    }

    /** 删除多条
     * @param Request $request
     */
    public function delete_more(Request $request)
    {
        $checked_id = $request->checked_id;
//        return $checked_id;

//        foreach ($checked_id as $id) {
        Purchase::destroy($checked_id);
//        }

    }
}
